package com.cn.sys.main.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.sys.frame.util.query.QueryBean;
import com.cn.sys.frame.util.query.QueryCondition;
import com.cn.sys.frame.web.BaseController;
import com.cn.sys.main.entity.SysFunction;
import com.cn.sys.main.entity.SysModule;
import com.cn.sys.main.service.SysFunctionService;
import com.cn.sys.main.service.SysModuleService;

@Controller
@RequestMapping("sys/module")
public class SysModuleController extends BaseController {

	@Autowired
	private SysModuleService sysModuleService;
	@Autowired 
	private SysFunctionService sysFunctionService;
	
	@RequestMapping(value = "/toList.do", method = RequestMethod.GET)
	public String toList(HttpServletRequest request, Model model){
		return "sys/module/moduleList";
	}
	
	@ModelAttribute("module")
	public SysModule createModule(){
		return new SysModule();
	}
	@RequestMapping(value = "/toAdd.do", method = RequestMethod.GET)
	public String toAdd(String id, Model model) throws Exception{
		if(StringUtils.isNotBlank(id)&&StringUtils.isNumeric(id)){
			SysModule module = sysModuleService.get(Long.valueOf(id));
			model.addAttribute("module", module);
		}
		model.addAttribute("parentModule", sysModuleService.getParentModules(id));
		return "sys/module/moduleAdd";
	}
	
	@RequestMapping(value = "/list.do", method = RequestMethod.POST)
	@ResponseBody
	public Object list() throws Exception{
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("rows", sysModuleService.getAllList());
		return map;
	}
	
	/*@InitBinder
	protected void initBinder(WebDataBinder binder){
		binder.setValidator(new ModuleValidator());
	}*/
	
	@RequestMapping(value = "/save.do", method = RequestMethod.POST)
	@ResponseBody
	public Object save( SysModule module) throws Exception{
		if(StringUtils.isBlank(module.getIsLeaf())){
			module.setIsLeaf("0");
		}
		if(module.getId()!=null){
			module = sysModuleService.update(module);
		}else {
			module = sysModuleService.save(module);
		}
		Map<String, Object> result = new HashMap<String, Object>(1);
		if(module==null){
			result.put("success", false);
			
		}else {
			result.put("success", true);
		}
		return result;
	}
	
	@RequestMapping(value = "/validateModuleName.do", method = RequestMethod.POST)
	@ResponseBody
	public Object validateModuleName(String id, String name) throws Exception{
		QueryCondition queryCondition = new QueryCondition();
		if(StringUtils.isNotBlank(id)){
			QueryBean qb = new QueryBean(" AND ID <> ?", "ID", Long.valueOf(id));
			queryCondition.setQueryBean(qb);
		}
		if(StringUtils.isNotBlank(name)){
			queryCondition.setQueryBean(new QueryBean(" AND MODULE_NAME = ?", "MODULE_NAME", name));
		}else {
			return false;
		}
		long count = sysModuleService.getCount(queryCondition);
		if(count>0){
			return false;
		}
		return true;
	}
	
	@RequestMapping(value = "/delete.do", method = RequestMethod.POST)
	@ResponseBody
	public Object delete(HttpServletRequest request, String id) throws Exception{
		Map<String, Object> result = new HashMap<String, Object>(2);
		if(StringUtils.isNotBlank(id)&&StringUtils.isNumeric(id)){
			QueryCondition queryCondition = new QueryCondition();
			QueryBean qb = new QueryBean("AND PARENT_ID = ?", "PARENT_ID", Long.valueOf(id));
			queryCondition.setQueryBean(qb);
			long queryCnt = sysModuleService.getCount(queryCondition);
			if(queryCnt<=0){
				int count = sysModuleService.delete(Long.valueOf(id));
				if(count == 1){
					result.put("success", true);
				}else {
					result.put("success", false);
				}
			}else{
				result.put("success", false);
				result.put("msg", "该模块下有子模块，请先删除子模块!");
			}
		}else {
			result.put("success", false);
		}
		return result;
	}
	
	@RequestMapping(value = "/getFunctionsByModuleId.do", method = RequestMethod.POST) 
	@ResponseBody
	public Object getFunctionsByModuleId(String moduleId) throws Exception{
		Map<String, Object> result = new HashMap<String, Object>(1);
		List<SysFunction> list = sysFunctionService.getFunctionsByModuleId(moduleId);
		result.put("rows", (list!=null)?list:new ArrayList<SysFunction>());
		return result;
	}
}
